Ext.namespace("org.microsite.user");

org.microsite.user.LoginWindow = Ext.extend(Ext.Window, {
    title : 'Enter you credentials',
    closable : false,
    modal : true,
    width : 300,
    height : 200,
    layout : 'fit',

    txtUid : null,
    txtPwd : null,
    btnOK : null,
    btnCancel : null,
    frm : null,
    
    initComponent : function() {
        this.txtUid = new Ext.form.TextField({
            name : 'uid',
            fieldLabel : 'User name',
            value : '',
            anchor : '100%'
        });
        this.txtPwd = new Ext.form.TextField({
            name : 'pwd',
            fieldLabel : 'Password',
            value : '',
            inputType : 'password',
            anchor : '100%'
        });
        this.btnOK = new Ext.Button({
            text : 'OK',
            handler : this.onOKClicked,
            scope : this
        });
        this.btnCancel = new Ext.Button({
            text : 'Cancel',
            handler : this.onCancelClicked,
            scope : this
        });
        
        this.frm = new Ext.FormPanel({
            bodyStyle : 'padding: 20px',
            items : [this.txtUid, this.txtPwd]
        });
        
        Ext.apply(this, {
            items : this.frm,
            buttons : [this.btnOK, this.btnCancel]
        });
        
        org.microsite.user.LoginWindow.superclass.initComponent.apply(this);

        this.addEvents({
            'login' : true
        });
    },
    
    onCancelClicked : function(btn, e) {
        this.close();
    },

    onOKClicked : function(btn, e) {
        win = this;
        UserRemote.login(this.txtUid.getValue(), this.txtPwd.getValue(), function(user) {
            if (user != null && user != 'null') {
                win.fireEvent('login', user.name);
                win.close();
            } else {
                Ext.MessageBox.alert('Login failed', 'No matching user name or password')
            }
        });
    }
})